#下载附件后，如果附件是doc格式，则需要将它转换成docx格式存储到磁盘中
import pandas as pd
import requests
import os
import win32com.client as win32
from urllib.parse import urlparse
from concurrent.futures import ThreadPoolExecutor, as_completed
from time import sleep
#根据附件地址下载附件到本地指定路径

# 读取 Excel 文件中的企业名称列表
input_file = 'F:\\knowledgeAttachFailedList.xlsx'
# 读取 Excel 文件
df = pd.read_excel(input_file)
urls = df['url'].tolist()
# 设置复杂的请求头信息
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    "Host": "www.cmde.org.cn",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}

# 创建下载目录
download_dir = r'F:\knowledge'
if not os.path.exists(download_dir):
    os.makedirs(download_dir)
def download_and_convert(url):
    try:
        # 解析URL以获取文件名
        parsed_url = urlparse(url)
        attach_name = os.path.basename(parsed_url.path)
        # 下载附件
        response = requests.get(url, headers=headers, timeout=20)  # 设置请求最大等待时间为20秒
        if response.status_code == 200:
            file_path = os.path.join(download_dir, attach_name)
            with open(file_path, 'wb') as f:
                f.write(response.content)
            return f"下载并转换成功: {attach_name}"
        else:
            return f"请求失败，状态码：{url} - {response.status_code} - {attach_name}"
    except Exception as e:
        return f'请求失败，错误信息: {str(e)} - {attach_name}'

# 使用多线程下载附件
with ThreadPoolExecutor(max_workers=10) as executor:  # 设置最大线程数为10
    future_to_url = {executor.submit(download_and_convert, url): url for url in urls}
    for future in as_completed(future_to_url):
        url = future_to_url[future]
        result = future.result()
        print(result)
        sleep(1)  # 设置请求间隔，避免被封禁

print("所有附件下载和转换完成")